import tkinter as tk
from tkinter import ttk
import draw
import sd as sd
import host
from pic2svg import rings
from draw import drawin_create
from PIL import Image, ImageTk
import subprocess

# 创建主窗口
root = tk.Tk()
root.title("绘图机器人上位机")
root.geometry("1000x800")

img_path = ""



def wrap_sd_work(positive_promt, negative_promt, url):
    global img_path
    img_path = sd.sd_work(positive_promt, negative_promt, url)
    img_open = Image.open(img_path)
    img_png = ImageTk.PhotoImage(img_open)
    label_img.config(image=img_png)
    label_img.image = img_png

# 选项卡1完成图片生成的工作
def tab1_setting():
    tab_control.add(tab1, text='图片生成')
    positive_promt = tk.StringVar()
    positive_promt.set("rabbit")
    negative_promt = tk.StringVar()
    negative_promt.set("low quality")
    url = tk.StringVar()
    url.set("http://127.0.0.1:7860")
    button_sd_init = tk.Button(tab1, text="SD初始化", command=sd.sd_init)
    button_sd_init.grid(row=0, column=0)
    button_sd_work = tk.Button(tab1, text="生成图像", command=lambda:wrap_sd_work(positive_promt.get(), negative_promt.get(),url.get()))
    button_sd_work.grid(row=0, column=1)
    tk.Label(tab1, text="正面提示词").grid(row=1, column=0, sticky= 'W')
    entry_positive_prompt = tk.Entry(tab1, width=100, textvariable=positive_promt)
    entry_positive_prompt.grid(row=2,column=0, columnspan=2)
    tk.Label(tab1, text="负面提示词").grid(row=3, column=0, sticky= "W")
    entry_negative_prompt = tk.Entry(tab1, width=100, textvariable=negative_promt)
    entry_negative_prompt.grid(row=4, column=0, columnspan=2)
    tk.Label(tab1, text="SD ip地址").grid(row=5,column=0,sticky="w")
    entry_ip = tk.Entry(tab1, textvariable=url, width=100)
    entry_ip.grid(row=6, column=0, columnspan=2, sticky="W")
    img_open = Image.open("./tmp/temp_file/none.png")
    img_png = ImageTk.PhotoImage(img_open)
    global label_img 
    label_img= tk.Label(tab1)
    label_img.config(image=img_png)
    label_img.image = img_png
    label_img.grid(row = 7, column=0, columnspan=2, sticky="W")
    

def warp_convert(p1 : int, p2 : int):
    rings(".\output\output.png", "img_canny.svg", layers_gate = [5000,3000,1200,p2],blur_size = p1)
    drawin_create('img_canny.svg', no_te = True)

#选项卡2完成
def tab2_setting():
    p1 = tk.StringVar()
    p1.set('0')
    p2 = tk.StringVar()
    p2.set('0')
    tk.Label(tab2, text="blur size:").grid(row=0,column=0,sticky="w")
    entry_1 = tk.Entry(tab2, textvariable=p1,width=100)
    entry_1.grid(row=1, column=0, sticky='w')
    tk.Label(tab2, text="layers gate threshold:").grid(row=2, column=0, sticky="w")
    entry_2 = tk.Entry(tab2, textvariable=p2, width=100)
    entry_2.grid(row=3, column=0, sticky='w')
    tab_control.add(tab2, text="指令生成")
    button_convert = tk.Button(tab2, text="把图片转换为绘图指令", width=100, command=lambda:warp_convert(int(p1.get()),int(p2.get())))
    button_convert.grid(row=4, column=0, sticky="w")
    


def arm_move(x,y,z):
    host.move(x.get(),y.get(),z.get())
    

def tab3_setting():
    button_link = tk.Button(tab3, text="连接机械臂", width=100, command=host.link)
    button_link.grid(row=0,column=0,sticky="w")
    button_link.grid
    tab_control.add(tab3, text='机械臂操纵')
    x = tk.StringVar()
    x.set("0")
    y = tk.StringVar()
    y.set("0")
    z = tk.StringVar()
    z.set("0")
    entry_x = tk.Entry(tab3, textvariable=x, width=100)
    entry_x.grid(row=1, column= 0, sticky="w")
    entry_y = tk.Entry(tab3, textvariable=y, width=100)
    entry_y.grid(row=2, column=0, sticky="w")
    entry_z = tk.Entry(tab3, textvariable=z, width=100)
    entry_z.grid(row=3, column=0, sticky="w")
    button_move = tk.Button(tab3, text="获取初始位置", width=100, command=host.get_current_posi)
    button_move.grid(row=4, column=0, sticky="w")
    button_movexz = tk.Button(tab3, text="基于纸面坐标系的平移", width=100, command=lambda:host.move(x.get(), y.get(), z.get()))
    button_movexz.grid(row=5, column=0, sticky="w")
    button_close = tk.Button(tab3, text="关闭连接", width=100, command=lambda:host.close())
    button_close.grid(row=6, column=0, sticky="w")
    button_up = tk.Button(tab3, text="抬笔", width=100, command=host.up)
    button_up.grid(row=7, column=0, sticky="w")
    button_down = tk.Button(tab3, text="落笔", width=100, command=host.down)
    button_down.grid(row=8, column=0, sticky="w")
    button_draw = tk.Button(tab3, text="go!!!!", width=20, command=host.go, font=('黑体', 24,'bold'))
    button_draw.grid(row=9, column=0, sticky="w")


# 创建并设置三个选项卡
tab_control = ttk.Notebook(root)
tab1 = ttk.Frame(tab_control)
tab1_setting()
tab2 = ttk.Frame(tab_control)
tab2_setting()
tab3 = ttk.Frame(tab_control)
tab3_setting()

# 将选项卡控件放置到主窗口中
tab_control.pack(expand=1, fill="both")

# 进入主循环
root.mainloop()
